/***************************************************************************
 * Slink.Generator -- Copyright Bit Armory Labs 2007
 *
 * FILE:     SlinkNamespace.cs, 41 code lines, [841 Bytes]
 * DATE:     8/26/2007 6:34 PM
 * MODIFIED: 9/2/2007 9:17 PM
 * PATH:     C:\Programming\Projects\Slink\Slink.Generator
 *
 * NAMESPACE: Slink.Generator
 * CLASS:     SlinkNamespace
 *
 * AUTHOR:   Brian Chavez (Cowboy), bchavez@bitarmory.com
 *
 *                        Licence
 *
 * This library, code generator, and source code
 * is licensed under the terms and conditions defined in the GNU Public
 * License. You are free to use it for non-commercial purposes.
 * If you are going to use any part of it in a commercial 
 * application, contact me and we can work something out.
 * You may use the code generated output of Slink and Slink
 * as a development tool in both commercial and 
 * non-commercial applications.
 *
 * THE SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS", WITHOUT WARRANTY
 * OF ANY KIND, EXPRESS OR IMPLIED. IN NO EVENT SHALL THE AUTHOR BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OF THIS
 * SOFTWARE.
 ***************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using TaHoGen.Generators;
using Slink.Core;
using System.IO;

namespace Slink.Generator
{
    public class SlinkNamespace
    {
        private Dictionary<string, SlinkClass> slinkClasses = new Dictionary<string, SlinkClass>();

        private string _namespace;

        public SlinkNamespace( string _namespace )
        {
            this._namespace = _namespace;
        }


        public string Namespace
        {
            get
            {
                return this._namespace;
            }
        }

        public Dictionary<string, SlinkClass> SlinkClasses
        {
            get
            {
                return this.slinkClasses;
            }
        }

        /// <summary>
        /// Assemble namespace generator
        /// </summary>
        /// <returns>Text generator</returns>
        public TextGenerator GetNamespaceTextGenerator()
        {
            TextGenerator classTextGenerators = null;
            foreach( SlinkClass slinkClass in this.slinkClasses.Values)
            {
                if( classTextGenerators == null )
                {
                    classTextGenerators = slinkClass.GetClassTextGenerator();
                }
                else
                {
                    classTextGenerators += slinkClass.GetClassTextGenerator();
                }
            }

            if( classTextGenerators == null )
            {
                return null;
            }
            else
            {
                TextGenerator namespaceTextGenerator = NamespaceTemplateFactory.CreateNamespaceTextGenerator( this.Namespace, classTextGenerators );
                return namespaceTextGenerator;
            }
        }


        /// <summary>
        /// Get namespace for aspx file
        /// </summary>
        public static string GetNamespace(string fullFileOrDirectory, NamespaceRulesElement rule, SlinkManagedWebProject project)
        {
            string resultNs = "";
            if( rule.NamespaceOverride.Length > 0 )
            {
                resultNs = rule.NamespaceOverride;
            }
            else if( rule.UseFolderNamespace )
            {
                string directoryPath = Path.GetDirectoryName( Path.GetFullPath( fullFileOrDirectory ) );

                string relativeUrl = directoryPath.Replace( project.VsWebProjectFullFilePathOnDisk, "" ).Trim( Path.DirectorySeparatorChar );

                resultNs = relativeUrl.Replace( Path.DirectorySeparatorChar, '.' ).Trim('.');
            }
            resultNs = project.ConfigFile.RootNamespace + "." + resultNs;
            return System.Text.RegularExpressions.Regex.Replace( resultNs, @"[^\w.]", "_" ).Trim('.');
        }

        
    }
}
